package com.hwua.view;

import com.hwua.domain.User;
import com.hwua.service.UserService;
import com.hwua.serviceimpl.UserServiceImpl;
import com.hwua.util.IdentifyingCodeTools;
import com.hwua.util.InputTools;

import java.util.List;

/**
 * 登录界面
 * @author admin
 */
public class LoginView extends AbstractView{
    @Override
    public AbstractView showView() {
        System.out.println("======登录=====>>>>>");
        System.out.println("用户名：");
        String name = InputTools.nextString();
        System.out.println("密  码：");
        String pwd = InputTools.nextString();
        while(true){
            //获取随机四位验证码
            int identifyingCode = IdentifyingCodeTools.createIdentifyingCode();
            System.out.println("验证码："+identifyingCode);
            System.out.println("请输入验证码：☟☟☟☟☟☟☟☟");
            int num = InputTools.checkIntInput(0, 9999);
            if(num==identifyingCode){
                break;
            }
            System.out.println("验证码错误，请重新输入：");
        }
        UserService us = new UserServiceImpl();
        List<User> userList = us.login(name, pwd);
        if(!userList.isEmpty() && "admin".equals(name)){
            nextView = new AdminView();
            System.out.println("登录成功，欢迎☞"+name+"☜♬♬♬");
        }else if(userList.isEmpty()){
            System.out.println("用户名或密码错误！（1：重新登录 2.注册）：");
            int choose = InputTools.checkIntInput(1, 2);
            switch (choose){
                case 1:
                    nextView = new LoginView();
                    break;
                case 2:
                    nextView = new RegisterView();
                    break;
                default:break;
            }
        }else{
            //登录成功跳转到用户主界面的同时利用UserView的有参构造传入当前用户的名字
            nextView = new UserView(name,pwd);
            System.out.println("登录成功，欢迎☞"+name+"☜♬♬♬");
        }
        return nextView;
    }
}
